import { finOrgALlTree } from '../../api/org'
import { getRoleTree } from '../../api/role'
import { getDictListByGroupCodes } from '../../api/dic'
import { getButtonsByMenuId } from '../../api/resource'
export default {
  name: 'common',
  state: {
    orgList: []
  },
  mutations: {
    setOrgList(state, list) {
      state.orgList = list
    }
  },
  getters: {

  },
  actions: {
    // 获取机构树
    getOrgList({ commit, rootState }, { domainId, orgId }) {
      return new Promise((resolve, reject) => {
        finOrgALlTree({ domainId, orgId }).then(res => {
          if (res.data && res.data.length > 0) {
            commit('setOrgList', res.data)
            resolve(res.data)
          } else {
            commit('setOrgList', [])
            resolve([])
          }
        }).catch(err => {
          reject(err)
        })
      })
    },
    // 获取多个字典值，传数组
    getDictListByGroupCodes({ commit, rootState }, { groupCodeList, isPubFlag }) {
      return new Promise((resolve, reject) => {
        getDictListByGroupCodes({ groupCodeList, isPubFlag }).then(res => {
          resolve(res)
        }).catch(err => {
          reject(err)
        })
      })
    },
    // 获取角色tree
    getRoleTree({ commit, rootState }) {
      return new Promise((resolve, reject) => {
        getRoleTree().then(res => {
          resolve(res.data)
        }).catch(err => {
          reject(err)
        })
      })
    },
    // 获取角色tree
    getButtonsByMenuId({ commit }, resourceId) {
      return new Promise((resolve, reject) => {
        getButtonsByMenuId(resourceId).then(res => {
          let obj = {}
          if (res.data && res.data.length > 0) {
            res.data.forEach(item => {
              obj[item.resCode] = true
            })
          }
          resolve(obj)
        }).catch(err => {
          reject(err)
        })
      })
    }
  }
}
